import os
from flask_session import Session
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import redis
from flask_wtf.csrf import CSRFProtect
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand


# 创建Flask应用程序实例
app = Flask(__name__)


class Config(object):
    """项目配置信息"""
    DEBUG = True
    KEY = "dsadadagkj3412lkr1dad;.,;'a"
    SECRET_KEY = os.getenv(KEY, default=None)

    # sessions储存信息配置
    SESSION_TYPE = "redis"  # 指定ｓｅｓｓｉｏｎｓ储存方式为ｒｅｄｉｓ
    SESSION_USE_SIGNER = True # 让ｓｅｓｓｉｏｎ中信息加密
    PERMANENT_SESSION_LIFETIME = 86400  # session 的有效期，单位是秒

    # add SQLALchemy info
    SQLALCHEMY_DATABASE_URI = "mysql://root:mysql@127.0.0.1:3306/information"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # add redis.db info
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = 6379


app.config.from_object(Config)

# open the CSRFprotect
CSRFProtect(app)

db = SQLAlchemy(app)

redis_store = redis.StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT)

Session(app)

# 创建ｍａｎｇｅｒ对象
manager = Manager(app)
Migrate(app, db)
# 添加迁移命令到ｄｂ中
manager.add_command('db', MigrateCommand)


@app.route('/')
def index():
    return 'index'


if __name__ == '__main__':
    # 运行开发web服务器
    manager.run()
